﻿using Castle.Components.DictionaryAdapter.Xml;
using netcore_comm.SqlSugar;
using netcore_service.Dto;
using netcore_service.IRepository;
using netcore_service.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.WebSockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace netcore_service.Repositories
{
    /// <summary>
    /// 
    /// </summary>
    public class SchoolRepository : SqlSugarRepository<SchoolInfo, netData>, ISchoolRepository
    {
        private readonly ISqlSugarFactory _sqlSugarFactory;
        public SchoolRepository(ISqlSugarFactory sqlSugarFactory) : base(sqlSugarFactory)
        {

            _sqlSugarFactory = sqlSugarFactory;
        }

        public async Task saveImage(SchoolInfo schoolInfo)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);
            var list = await db.Queryable<School_Image>().Where(e => e.SchoolId == schoolInfo.Id).ToListAsync();
            await db.Deleteable(list).ExecuteCommandAsync();
            if (schoolInfo.imgList != null)
            {

                foreach (var item in schoolInfo.imgList)
                {
                    item.SchoolId = schoolInfo.Id;
                    item.CreateName = schoolInfo.Name;
                    item.CreateTime = DateTime.Now;
                    item.IsDel = false;
                    //School_Image school_Image = new School_Image
                    //{
                    //    SchoolId = schoolInfo.Id,
                    //    Url = item.Url,
                    //    Name = item.Name,
                    //    CreateName = schoolInfo.Name,
                    //    CreateTime = DateTime.Now,
                    //    IsDel = false
                    //};
                }
            }
            await db.Insertable(schoolInfo.imgList).ExecuteCommandAsync();

        }

        public async Task<SchoolInfoDto> SchoolById(string id)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            var info = await db.Queryable<SchoolInfo>().
               Where(e => !e.IsDel && e.Id == id)
               .Select(s => new SchoolInfoDto()
               {
                   Id = s.Id,
                   Name = s.Name,
                   City = s.City,
                   SchoolCode = s.SchoolCode,
                   CellPhone = s.CellPhone,
                   Address = s.Address,
                   WebSiteAddress = s.WebSiteAddress,
                   Schoolbxxz = s.Schoolbxxz,
                   Schoolspjs = s.Schoolspjs,
                   Schoolzsms = s.Schoolzsms,
                   Schoolksxs = s.Schoolksxs,
                   SchoolYxcc = s.SchoolYxcc,
                   AskAddress = s.AskAddress,
                   ApplyForAddress = s.ApplyForAddress,
                   Logo = s.Logo,
                   state = s.state,
                   type = s.type,
                   Schoolzyjs = s.Schoolzyjs                  
               }).FirstAsync();
            info.imgList = GetImgList(id);
            return info;
        }

        public List<School_Image> GetImgList(string id)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));

            return db.Queryable<School_Image>().Where(e => e.SchoolId == id).ToList();
        }

        public async Task<SchoolDetailDto> SchoolDetail(string id, string type)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            var info = db.Queryable<SchoolInfo>().
              Where(e => !e.IsDel && e.Id == id);
            var detail = new SchoolDetailDto();
            switch (type)
            {
                case "xxjj"://学校简介
                    detail = await info.Select(e => new SchoolDetailDto { Id = e.Id, Content = e.SchoolIntroduce }).FirstAsync();
                    break;
                case "zszc"://招生简章
                    detail = await info.Select(e => new SchoolDetailDto { Id = e.Id, Content = e.Schoolzsjz }).FirstAsync();
                    break;
                case "zyjh"://招生计划
                    detail = await info.Select(e => new SchoolDetailDto { Id = e.Id, Content = e.Schoolzyjs }).FirstAsync();
                    break;
                case "zxzx"://我要咨询
                    detail = await info.Select(e => new SchoolDetailDto { Id = e.Id, Content = e.AskAddress }).FirstAsync();
                    break;
                case "lnfs"://历年分数
                    detail = await info.Select(e => new SchoolDetailDto { Id = e.Id, Content = e.school_fenshu }).FirstAsync();
                    break;
                case "zyjs"://专业介绍
                    detail = await info.Select(e => new SchoolDetailDto { Id = e.Id, Content = e.Schoolzyjs }).FirstAsync();
                    break;
                default:
                    detail = null;
                    break;
            }
            return detail;
        }

        public async Task<(dynamic,int)> SchoolInfoList(SchoolInfoPo po, List<string> school)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            RefAsync<int> totalCount = 0;
            var info = await db.Queryable<SchoolInfo>().
                Where(e => !e.IsDel && e.type == po.type).
                WhereIF(school.Count > 0, e => school.Contains(e.Name)).
                WhereIF(!string.IsNullOrEmpty(po.state), e => e.state == po.state).
                WhereIF(!string.IsNullOrEmpty(po.CityParam), e => po.CityParam.Contains(e.City)).
                WhereIF(!string.IsNullOrEmpty(po.Name), e => e.Name.Contains(po.Name)).
                WhereIF(!string.IsNullOrEmpty(po.ksxs), e => e.Schoolksxs == po.ksxs)
                .Select(s => new
                {
                    s.Id,
                    s.Name,
                    s.City,
                    s.SchoolCode,
                    s.CellPhone,
                    s.Address,
                    s.WebSiteAddress,
                    s.Schoolbxxz,
                    s.Schoolspjs,
                    s.Schoolzsms,
                    s.Schoolksxs,
                    s.SchoolYxcc,
                    s.AskAddress,
                    s.ApplyForAddress,
                    s.Logo,
                    s.ModifyDate,
                    s.state
                }).OrderBy(e => e.SchoolCode).ToOffsetPageAsync(po.page, po.pageSize, totalCount);
            return (info,totalCount);
        }

        public async Task<List<SchoolInfoDtoM>> SchoolList(SchoolInfoPo po)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            var info = await db.Queryable<SchoolInfo>().
                Where(e => !e.IsDel && e.type == po.type ) //&& e.state == "1"
                .Select<SchoolInfoDtoM>(s => new SchoolInfoDtoM()
                {
                    Id = s.Id,
                    Name = s.Name,
                    City = s.City,
                    SchoolCode = s.SchoolCode,
                    CellPhone = s.CellPhone,
                    Address = s.Address,
                    WebSiteAddress = s.WebSiteAddress,
                    Schoolbxxz = s.Schoolbxxz,
                    Schoolspjs = s.Schoolspjs,
                    Schoolzsms = s.Schoolzsms,
                    Schoolksxs = s.Schoolksxs,
                    SchoolYxcc = s.SchoolYxcc,
                    //AskAddress = s.AskAddress,
                    //ApplyForAddress = s.ApplyForAddress,
                    Logo = s.Logo,
                    Sort = s.Sort,
                    type = s.type,
                    state = s.state,
                }).OrderBy(e => e.SchoolCode).ToListAsync();
            return info;
        }

        public async Task<int> AddCollection(Collection data)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);

            var school = await db.Queryable<SchoolInfo>().Where(e => e.Id == data.schoolId).AnyAsync();
            if (!school)
            {
                return 0;
            }
            //先判断已存在，不存在才新增
            var u = await db.Queryable<Collection>().Where(e => e.schoolId == data.schoolId && e.openid == data.openid).AnyAsync();
            if (u)
            {
                return 1;
            }
            else
            {
                return await db.Insertable<Collection>(data).ExecuteCommandAsync();
            }
        }

        public async Task<List<SchoolInfoDto>> getCollection(string uid)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));

            var from = await db.Queryable<Collection>()
                .LeftJoin<SchoolInfo>((c, s) => c.schoolId == s.Id)
                .Where((c, s) => c.openid == uid).Select((c, s) => new SchoolInfoDto()
                {
                    Id = s.Id,
                    Name = s.Name,
                    Logo = s.Logo,
                    Schoolbxxz = s.Schoolbxxz,
                    SchoolYxcc = s.SchoolYxcc,
                    Schoolzsms = s.Schoolzsms,
                }).ToListAsync();
            return from;
        }

        public async Task<List<SchoolInfoDto>> SchoolListByIds(List<string> ids)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return await db.Queryable<SchoolInfo>().Where(e => ids.Contains(e.Id)).Select(e => new SchoolInfoDto()
            {
                Id = e.Id,
                Name = e.Name,
                Logo = e.Logo,
                Schoolbxxz = e.Schoolbxxz,
                SchoolYxcc = e.SchoolYxcc,
                Schoolzsms = e.Schoolzsms,
            }).ToListAsync();
        }

        public async Task<int> cancelCollect(string id, string uid)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData), DBOperateType.Write);
            return await db.Deleteable<Collection>().Where(e => e.schoolId == id && e.openid == uid).ExecuteCommandAsync();
        }

        public async Task<bool> getCollectionbyId(string schoolId, string uid)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return await db.Queryable<Collection>().Where(e => e.openid == uid && e.schoolId == schoolId).AnyAsync();
        }

        public int getCollectionbySchool(string schoolId)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));

            return db.Queryable<Collection>().Where(e => e.schoolId == schoolId).Count();
        }

        public async Task<SchoolInfo> getSchoolIdByOpenid(string uid)
        {
            var db = _sqlSugarFactory.GetInstance(nameof(netData));
            return await db.Queryable<SchoolInfo>().Where(e => e.OpenId.Contains(uid)).FirstAsync();
        }
        //public async Task<List<SchoolInfo>> SchoolList()
        //{
        //    var db = _sqlSugarFactory.GetInstance(nameof(netData));
        //    return await db.Queryable<SchoolInfo>().ToListAsync();
        //}

    }
}
